package com.xiaomage.view.draw8

import android.graphics.*
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.SeekBar
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var beforeBitmap:Bitmap
    private lateinit var emptyBitmap:Bitmap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        beforeBitmap=BitmapFactory.decodeResource(resources,R.drawable.dog)
        emptyBitmap= Bitmap.createBitmap(beforeBitmap.width,
                beforeBitmap.height,Bitmap.Config.ARGB_8888)
        val myView=MyView(this)
        val param=LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                4000
        )
        myView.layoutParams=param
        ll_zong.addView(myView,1)

        seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                Log.e("滑动竖直",p1.toString())
                img.setImageBitmap(getBitmap(p1.toFloat()))
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
            }

        })
    }

    private fun getBitmap(progress:Float):Bitmap{
        val canvas=Canvas(emptyBitmap)
        val paint= Paint()
        paint.isAntiAlias=true
        val colorMatrix=ColorMatrix()
        //设置饱和度
        colorMatrix.setSaturation(progress)
        paint.colorFilter=ColorMatrixColorFilter(colorMatrix)
        canvas.drawBitmap(beforeBitmap,0f,0f,paint)
        return emptyBitmap
    }
}
